package cn.iocoder.yudao.module.member.controller.admin.activation;

import cn.iocoder.yudao.module.member.controller.admin.activation.userVo.UserActivationPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activation.userVo.UserActivationRespVO;
import cn.iocoder.yudao.module.member.controller.admin.activation.userVo.UserActivationSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activation.UserActivationDO;
import cn.iocoder.yudao.module.member.service.activation.UserActivationService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.activation.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.activation.ActivationDO;
import cn.iocoder.yudao.module.member.service.activation.ActivationService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 我的激活码")
@RestController
@RequestMapping("/member/my-activation")
@Validated
public class UserActivationController {

    @Resource
    private UserActivationService activationService;

    @PostMapping("/create")
    @Operation(summary = "创建我的激活码")
    public CommonResult<Long> createActivation(@Valid @RequestBody UserActivationSaveReqVO createReqVO) {
        return success(activationService.createActivation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新我的激活码")
    public CommonResult<Boolean> updateActivation(@Valid @RequestBody UserActivationSaveReqVO updateReqVO) {
        activationService.updateActivation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除我的激活码")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteActivation(@RequestParam("id") Long id) {
        activationService.deleteActivation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得我的激活码")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<UserActivationRespVO> getActivation(@RequestParam("id") Long id) {
        UserActivationDO activation = activationService.getActivation(id);
        return success(BeanUtils.toBean(activation, UserActivationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得我的激活码分页")
    public CommonResult<PageResult<UserActivationRespVO>> getActivationPage(@Valid UserActivationPageReqVO pageReqVO) {
        PageResult<UserActivationDO> pageResult = activationService.getActivationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserActivationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出我的激活码 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportActivationExcel(@Valid UserActivationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserActivationDO> list = activationService.getActivationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "我的激活码.xls", "数据", ActivationRespVO.class,
                        BeanUtils.toBean(list, ActivationRespVO.class));
    }

}